﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShipSwitchItem : MonoBehaviour
{
    public Text m_Name2;
    public Image m_Perview;
    public Button m_Select;
    public Button m_StoneCutting;
    public Text m_SelectText;
    public ShipSwitchPanel m_Panel;
    public int Index=0;
    public ShipDescriptionPanel m_ShipDescriptionPanel;
    public Ship Ship {
        set
        {
            m_StoneCutting.gameObject.SetActive(false);
            m_Ship = value;
            m_Name2.text = m_Ship.m_ShipStruct.m_Name;
            m_Perview.sprite = m_Ship.GetComponent<SpriteRenderer>().sprite;
            Stone stone = m_Ship as Stone;
            if (stone)
            {
                m_StoneCutting.gameObject.SetActive(true);
            }
        }
        get { return m_Ship; }
    }

    Ship m_Ship;
    void Start()
    {

        if (Player.Instance.m_DriveLevel > m_Ship.m_ShipStruct.m_LicenseLevel)
        {
            m_SelectText.color = new Vector4(1, 0, 0, 1);
            m_SelectText.text = "需要"+ m_Ship.m_ShipStruct.m_LicenseLevel + "级许可证";
        }
            m_Select.onClick.AddListener(onSelectedClicked);
        m_StoneCutting.onClick.AddListener(OnStoneCutting);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void onSelectedClicked()
    {
        if (Player.Instance.m_DriveLevel > m_Ship.m_ShipStruct.m_LicenseLevel)
        {
            MessageShow.Instance.Messaage("你的驾照等级不够，也许你应该去驾驶学院看看！");
            return;
        }
        m_Panel.m_ShipSwitcherExpandB.SetActive(true);
        m_Panel.m_ShipSwitcherReduceB.SetActive(false);
        m_Panel.gameObject.SetActive(false);
        Player.Instance.SelectShip = Index;
    }
    public void OnPerviewClicked()
    {
        if (m_ShipDescriptionPanel)
        {
            m_ShipDescriptionPanel.gameObject.SetActive(true);
            m_ShipDescriptionPanel.Ship = m_Ship;
            m_ShipDescriptionPanel.GetComponent<RectTransform>().position = UtilsUnity.GetPositionInScreen(Input.mousePosition, m_ShipDescriptionPanel.GetComponent<RectTransform>().sizeDelta);
        }
    }

    public void OnStoneCutting()
    {
        Stone stone = m_Ship as Stone;
        if (stone)
        {
            if(Player.Instance.GetCurrentShip() == m_Ship)
            {
                Player.Instance.SelectShip = -1;
            }
            stone.StoneCutting();
            Destroy(gameObject);
            m_Panel.Ding();
        }
    }
}
